﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;


public partial class Employee_Rent_RentCar : System.Web.UI.Page
{
 
    DBAcess db = new DBAcess();
    float insuranceCost = 0, totalPrice=0;
    String carid = "",licenseNumber="", plateNumber = "", brandName = "", carMake = "", carColor = "", carSpeed = "", carTariff = "";
    String carInsuranceType = "", carInsuranceValue = "", carStartDate="", carReturnDate="";
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Book a Vehicle | Nexus-Z Auto Rental";
        if (Session["employeeID"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        Cancel.CausesValidation = false;
        Search.CausesValidation = false;

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        CarID.Text = row.Cells[1].Text;
        Session["carID"] = CarID.Text;
        PlateNumber.Text = row.Cells[2].Text;
        Session["plateNumber"] = PlateNumber.Text;
        Brand.Text = row.Cells[3].Text;
        Session["brand"] = Brand.Text;
        Make.Text = row.Cells[4].Text;
        Session["make"] = Make.Text;
        Color.Text = row.Cells[5].Text;
        Session["color"] = Color.Text;
        Speed.Text = row.Cells[6].Text;
        Session["speed"] = Speed.Text;
        Tariff.Text = row.Cells[7].Text;
        Session["tariff"] = Tariff.Text;
    }
    protected void LicenseNo_TextChanged(object sender, EventArgs e)
    {
        Label1.Text = "";
        MySqlCommand cmd = new MySqlCommand("select license_no,first_name,last_name from customers where license_no like '%"+LicenseNo.Text+"%';", db.returnCon());
        db.open();
        MySqlDataReader dr = cmd.ExecuteReader();
        if (!dr.HasRows)
        {
            Label1.Text = "Customer doesnot exists";
        }
        else
        {
            dr.Read();

            Session["licenseNumber"] = dr.GetString(0);
            Session["name"] = dr.GetString(1) +" "+ dr.GetString(2);
        }
        dr.Close();
        db.close();
    }
    protected void Book_Click(object sender, EventArgs e)
    {
        Session["insuranceType"] = Insurance.SelectedItem.Text;
        Session["startDate"] = StartDate.Text;
        Session["returnDate"] = ReturnDate.Text;
        Session["insuranceCost"] = returnInsuranceCost(int.Parse(Insurance.SelectedValue));
        Session["totalAmount"] = getTotalAmount(int.Parse(Insurance.SelectedValue));

        Session["insuranceID"] = Insurance.SelectedValue;
        Response.Redirect("~/Employee/Rent/ConfirmBooking.aspx");

    }

    protected float getTotalAmount(int id)
    {
        TimeSpan time = Convert.ToDateTime(ReturnDate.Text).Subtract(Convert.ToDateTime(StartDate.Text));
        int noOfDays = time.Days;
        float totalAmount = 0;

        totalAmount = (noOfDays * (float.Parse(Tariff.Text) + returnInsuranceCost(int.Parse(Insurance.SelectedValue))));
       return totalAmount;
    }

   protected float returnInsuranceCost(int id)
    {
        float cost = 0;
        db.open();
        MySqlCommand cmd = new MySqlCommand("select cost from insurance where insurance_id=" + id + ";", db.returnCon());
        MySqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            cost = dr.GetFloat(0);
        }
        db.close();
        return cost;
    }

   protected void Search_Click(object sender, EventArgs e)
   {
       SqlDataSource3.SelectCommand = "select plate_no,car_id,make,brand,color,speed,tariff from car where brand='" + DropDownList1.SelectedItem.Text + "' and make='" + DropDownList2.SelectedItem.Text + "';";
   }
}